package com.ruoyi.mushcoin.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.orm.core.page.PageQuery;
import com.ruoyi.common.orm.core.page.TableDataInfo;
import com.ruoyi.common.orm.core.service.impl.BaseServiceImpl;
import com.ruoyi.common.core.utils.DateUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.mushcoin.mapper.TgUserLoginLogMapper;
import com.ruoyi.mushcoin.domain.TgUserLoginLog;
import com.ruoyi.mushcoin.domain.bo.TgUserLoginLogBo;
import com.ruoyi.mushcoin.domain.vo.TgUserLoginLogVo;
import com.ruoyi.mushcoin.service.ITgUserLoginLogService;

import static com.ruoyi.mushcoin.domain.table.TgUserLoginLogTableDef.TG_USER_LOGIN_LOG;

/**
 * TG游戏登录日志Service业务层处理
 *
 * @author billy
 * 2024-10-18
 */
@Service
public class TgUserLoginLogServiceImpl extends BaseServiceImpl<TgUserLoginLogMapper, TgUserLoginLog> implements ITgUserLoginLogService {
    @Resource
    private TgUserLoginLogMapper tgUserLoginLogMapper;

    @Override
    public QueryWrapper query() {
        return super.query().from(TG_USER_LOGIN_LOG);
    }

    private QueryWrapper buildQueryWrapper(TgUserLoginLogBo tgUserLoginLogBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(TG_USER_LOGIN_LOG.USER_ID.eq(tgUserLoginLogBo.getUserId()));
        queryWrapper.and(TG_USER_LOGIN_LOG.FIRST_NAME.like(tgUserLoginLogBo.getFirstName()));
        queryWrapper.and(TG_USER_LOGIN_LOG.LAST_NAME.like(tgUserLoginLogBo.getLastName()));
        queryWrapper.and(TG_USER_LOGIN_LOG.USER_NAME.like(tgUserLoginLogBo.getUserName()));
        queryWrapper.and(TG_USER_LOGIN_LOG.IS_PREMIUM.eq(tgUserLoginLogBo.getIsPremium()));
        queryWrapper.and(TG_USER_LOGIN_LOG.MUSHCOIN.eq(tgUserLoginLogBo.getMushcoin()));
        queryWrapper.and(TG_USER_LOGIN_LOG.CREATE_TIME.between(tgUserLoginLogBo.getParams().get("beginCreateTime"), tgUserLoginLogBo.getParams().get("endCreateTime")));
        queryWrapper.and(TG_USER_LOGIN_LOG.INVITE_REWARDS_TOTAL.eq(tgUserLoginLogBo.getInviteRewardsTotal()));
        queryWrapper.and(TG_USER_LOGIN_LOG.SAVE_GAME_COUNT.eq(tgUserLoginLogBo.getSaveGameCount()));
        queryWrapper.and(TG_USER_LOGIN_LOG.GOLD.eq(tgUserLoginLogBo.getGold()));
        queryWrapper.and(TG_USER_LOGIN_LOG.DIAMOND.eq(tgUserLoginLogBo.getDiamond()));
        queryWrapper.and(TG_USER_LOGIN_LOG.RANK_COIN.eq(tgUserLoginLogBo.getRankCoin()));
        queryWrapper.and(TG_USER_LOGIN_LOG.IP_ADDRESS.eq(tgUserLoginLogBo.getIpAddress()));
        queryWrapper.and(TG_USER_LOGIN_LOG.IP_ADDRESS_NAME.like(tgUserLoginLogBo.getIpAddressName()));

        return queryWrapper;
    }

    /**
     * 查询TG游戏登录日志
     *
     * @param id TG游戏登录日志主键
     * @return TG游戏登录日志
     */
    @Override
    public TgUserLoginLogVo selectById(Long id) {
        return this.getOneAs(query().where(TG_USER_LOGIN_LOG.ID.eq(id)), TgUserLoginLogVo.class);

    }

    /**
     * 查询TG游戏登录日志列表
     *
     * @param tgUserLoginLogBo TG游戏登录日志Bo
     * @return TG游戏登录日志集合
     */
    @Override
    public List<TgUserLoginLogVo> selectList(TgUserLoginLogBo tgUserLoginLogBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(tgUserLoginLogBo);
        return this.listAs(queryWrapper, TgUserLoginLogVo.class);
    }

    /**
     * 分页查询TG游戏登录日志列表
     *
     * @param tgUserLoginLogBo TG游戏登录日志Bo
     * @return 分页TG游戏登录日志集合
     */
    @Override
    public TableDataInfo<TgUserLoginLogVo> selectPage(TgUserLoginLogBo tgUserLoginLogBo) {

        QueryWrapper queryWrapper = buildQueryWrapper(tgUserLoginLogBo);
        queryWrapper.select("id,user_id,first_name,last_name,user_name,is_premium,mushcoin,create_time,invite_rewards_total,save_game_count,gold,diamond,rank_coin,ip_address,ip_address_name");
        Page<TgUserLoginLogVo> page = this.pageAs(PageQuery.build(), queryWrapper, TgUserLoginLogVo.class);
        return TableDataInfo.build(page);
    }

    /**
     * 新增TG游戏登录日志
     *
     * @param tgUserLoginLogBo TG游戏登录日志Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(TgUserLoginLogBo tgUserLoginLogBo) {
        TgUserLoginLog tgUserLoginLog = MapstructUtils.convert(tgUserLoginLogBo, TgUserLoginLog.class);


        return this.save(tgUserLoginLog);//使用全局配置的雪花算法主键生成器生成ID值
    }

    /**
     * 新增TG游戏登录日志，前台提供主键值，一般用于导入的场合
     *
     * @param tgUserLoginLogBo TG游戏登录日志Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insertWithPk(TgUserLoginLogBo tgUserLoginLogBo) {
        TgUserLoginLog tgUserLoginLog = MapstructUtils.convert(tgUserLoginLogBo, TgUserLoginLog.class);


        return tgUserLoginLogMapper.insertWithPk(tgUserLoginLog) > 0;//前台传来主键值
    }

    /**
     * 修改TG游戏登录日志
     *
     * @param tgUserLoginLogBo TG游戏登录日志Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(TgUserLoginLogBo tgUserLoginLogBo) {
        TgUserLoginLog tgUserLoginLog = MapstructUtils.convert(tgUserLoginLogBo, TgUserLoginLog.class);
        if (ObjectUtil.isNotNull(tgUserLoginLog) && ObjectUtil.isNotNull(tgUserLoginLog.getId())) {
            boolean updated = this.updateById(tgUserLoginLog);
            return updated;
        }
        return false;
    }


    /**
     * 批量删除TG游戏登录日志
     *
     * @param ids 需要删除的TG游戏登录日志主键集合
     * @return 结果:true 删除成功，false 删除失败
     */
    @Transactional
    @Override
    public boolean deleteByIds(Long[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }

}
